/*
 * $Id: ProdiagsTaskParamsMap.java,v 1.3 2009/08/20 20:09:42 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.prodiags;

import java.util.Arrays;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import com.ge.healthcare.autosc.common.database.dao.IRequestPropertiesDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.exceptions.BasicException;

public class ProdiagsTaskParamsMap {

	private Hashtable<Integer, ProdiagsScheduleTask> prodiagsTaskHash = new Hashtable<Integer, ProdiagsScheduleTask>();
	private ActRequest request = null;
	private Map<String, String> requestProperties = null;
	private static IRequestPropertiesDAO requestPropertiesDAO = null;
	
	/**
	 * This constructor is for Spring initiation.
	 */
	protected ProdiagsTaskParamsMap() {}
	
	public ProdiagsTaskParamsMap(ActRequest request) 
		throws BasicException {
		this.request = request;
		load();
	}
	
	public ProdiagsTaskParamsMap(Map<String, String> requestProperties)
		throws BasicException {
		this.requestProperties = requestProperties;
		load();
	}
	
	public ProdiagsScheduleTask getProdiagsTask(Integer index) {
		return prodiagsTaskHash.get(index);
	}
	
	public Integer[] getProdiagsTaskIndexes() {
		Integer[] taskIndexArray = prodiagsTaskHash.keySet().toArray(new Integer[0]); 
		Arrays.sort(taskIndexArray);
		return taskIndexArray;
	}
	
	public void setActRequestPropertiesDAO(IRequestPropertiesDAO requestPropertiesDAO) {
		ProdiagsTaskParamsMap.requestPropertiesDAO = requestPropertiesDAO;
	}
	
	private void load() throws BasicException {
		loadRequestProperties();
		if(requestProperties != null) {
			Set<String> propNames = requestProperties.keySet();
			for(String propName: propNames) {
				addParam(propName, requestProperties.get(propName));
			}
		}
	}
	
	private void loadRequestProperties() throws BasicException {
		if(requestProperties == null) {
			if(request != null) {
				requestProperties = requestPropertiesDAO.getRequestPropertiesByRequestId(request.getRequestId());
			} else {
				throw new BasicException("ActRequest is not defined");
			}
		}
	}
	
	private void addParam(String name, String value) {
		int idx = name.lastIndexOf("_");
		if(idx > 0) {
			Integer taskIndex = null;
			String id = name.substring(idx+1);
			try {
				taskIndex = new Integer(id);
			} catch (NumberFormatException nfe) {
				// Property may be for something else, skip
			}

			//If index is valid
			if(taskIndex != null) {
				String paramName = name.substring(0, idx);
				ProdiagsScheduleTask scheduleTask = prodiagsTaskHash.get(taskIndex);
				if(scheduleTask == null) {
					scheduleTask = new ProdiagsScheduleTask();
					prodiagsTaskHash.put(taskIndex, scheduleTask);
				}
				scheduleTask.addParam(paramName.toUpperCase(), value);
				scheduleTask.setId(id);
			}
		}

	}
}
